package com.example.mainstream.widget;

import java.util.ArrayList;
import java.util.List;

import com.alibaba.fastjson.JSONObject;
import com.myapp.activity.GoodsCarActivity.ClearCarThread;
import com.myapp.activity.GoodsCarActivity.GoodsCarListThread;
import com.myapp.activity.LoginActivity.LoginThread;
import com.myapp.common.HttpUtil;
import com.myweb.domain.Note;
import com.myweb.domain.UserOrder;
import com.myweb.domain.User;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

public class OrderCreateActivity extends ActionBarActivity {

	private Spinner ordercreate_sendtype;
	
	private Spinner ordercreate_paytype;
	
	private EditText ordercreate_sendtime;

	private TextView ordercreate_sumprice;
	
	private EditText ordercreate_address;
	
	private EditText ordercreate_tel;
	
	private EditText ordercreate_recusername;

	private List<String> data_list;

	private ArrayAdapter<String> arr_adapter;
	
	private List<String> data_list1;

	private ArrayAdapter<String> arr_adapter1;

	private Button ordercreate_createbtn;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.activity_order_create);

		Intent intent = getIntent();

		String sumprice = intent.getStringExtra("sumprice");

		ordercreate_sumprice = (TextView) findViewById(R.id.ordercreate_sumprice);	

		ordercreate_sumprice.setText(sumprice);

		ordercreate_sendtime = (EditText) findViewById(R.id.ordercreate_sendtime);

		ordercreate_sendtype = (Spinner) findViewById(R.id.ordercreate_sendtype);

		// ����
		data_list = new ArrayList<String>();
		data_list.add("���۵���ȡ");
		data_list.add("�ͻ�����");
		 

		// ������
		arr_adapter = new ArrayAdapter<String>(this,
				android.R.layout.simple_spinner_item, data_list);
		// ������ʽ
		arr_adapter
				.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		// ����������
		ordercreate_sendtype.setAdapter(arr_adapter);
		
		ordercreate_paytype = (Spinner) findViewById(R.id.ordercreate_paytype);

		// ����
		data_list1 = new ArrayList<String>();
		data_list1.add("����֧��");
		data_list1.add("��������");
		 

		// ������
		arr_adapter1 = new ArrayAdapter<String>(this,
				android.R.layout.simple_spinner_item, data_list1);
		// ������ʽ
		arr_adapter1
				.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		// ����������
		ordercreate_paytype.setAdapter(arr_adapter1);

		ordercreate_address = (EditText) findViewById(R.id.ordercreate_address);
		
		ordercreate_tel = (EditText) findViewById(R.id.ordercreate_tel);
		
		ordercreate_recusername = (EditText) findViewById(R.id.ordercreate_recusername);
		
		ordercreate_sumprice = (TextView) findViewById(R.id.ordercreate_sumprice);
		
		ordercreate_createbtn = (Button) findViewById(R.id.ordercreate_createbtn);

		ordercreate_createbtn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {

				Thread createOrderThread = new Thread(new CreateOrderThread());

				createOrderThread.start();
			}
		});

	}

	// ע����߳�
	class CreateOrderThread implements Runnable {

		public void run() {
			try {

				UserOrder order = new UserOrder();

				order.setAddress(ordercreate_address.getText().toString());

				order.setTel(ordercreate_tel.getText().toString());
				
				order.setSumprice(Float.parseFloat(ordercreate_sumprice.getText().toString()));
									 				
				order.setSendtype( ordercreate_sendtype.getSelectedItem().toString());
				
				order.setSendtime(ordercreate_sendtime.getText().toString());
				
				order.setPaytype(ordercreate_paytype.getSelectedItem().toString());
							
				MyApplication myApp = (MyApplication) getApplication(); // ����Զ����Ӧ�ó���MyApp

				order.setUserid(myApp.getUser().getId());

				order.setRecusername(ordercreate_recusername.getText().toString());
				
				System.out.println("=============="
						+ JSONObject.toJSONString(order));

				JSONObject paraObj = new JSONObject();

				paraObj.put("action", "create");

				paraObj.put("order", JSONObject.toJSONString(order));

				String result = HttpUtil.getJsonFromServlet(paraObj.toString(),
						"UserOrderService");

				if (result.equals("ok")) {

					Message msg = new Message();

					msg.what = 1;// �ɹ�

					handler.sendMessage(msg);
				}

			} catch (Exception ex) {

				System.out.println("ex: " + ex.getMessage());

				Message msg = new Message();

				msg.what = 2;// �����쳣

				handler.sendMessage(msg);

			}
		}
	}

	// Handler
	Handler handler = new Handler() {

		public void handleMessage(Message msg) {

			super.handleMessage(msg);

			switch (msg.what) {

			case 1:			 

				Toast.makeText(getApplicationContext(), "�����ɹ�!",
						Toast.LENGTH_SHORT).show();
				
				OrderCreateActivity.this.finish();
				
			//	Intent intent3 = new Intent(OrderCreateActivity.this,
				///		OrderActivity.class);
				// ����Activity
				//startActivity(intent3);
				
				break;

			case 2:
				 
				Toast.makeText(getApplicationContext(), "����ʧ�ܣ���������!",
						Toast.LENGTH_SHORT).show();
				
				
				break;
			}

		}
	};

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {

		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
}
